import requests
import json


class GetData():
    def __init__(self):
        self.web_url = "http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=100"
        self.header = {
            "Referer": "http://www.cwl.gov.cn/ygkj/wqkjgg/ssq/",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
        }

    def get_data(self, clolr_type):
        ball_list = []
        try:
            reponse = requests.get(self.web_url, headers=self.header)
            content = reponse.text
            data = json.loads(content).get("result")
            for x in data:
                line_str = json.dumps(x)
                line_dict = json.loads(line_str)
                if "red" == clolr_type:
                    temp_list = line_dict["red"].split(",")
                    for num in temp_list:
                        ball_list.append(int(num))
                elif "blue" == clolr_type:
                    ball_list.append(int(line_dict["blue"]))
                else:
                    print("color type error")
                print(str(line_dict["code"]) + " : " + str(line_dict["red"]) + " : " + line_dict["blue"])
        except IOError:
            print("get data error")
        return ball_list

d = GetData()
list_red = d.get_data("red")
list_blue = d.get_data("blue")
